﻿package nl.joeyvandijk
{
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.net.URLRequest;
	import flash.events.IEventDispatcher;
	import flash.events.EventDispatcher;
	import flash.events.HTTPStatusEvent;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	
	public class Pics extends EventDispatcher
	{
		private var temp:Array = new Array();
		private var item:uint = 0;
		public var items:Array = new Array();
		private var loader:Loader;
		private var isLoading:Boolean = false;
		
		private var percent:uint = 0;
		private var itemsLoaded:uint = 0;
		private var amount:uint = 0;
		public var progress:uint = 0;
		
		public function Pics()
		{
			//load pics
		}
		
		public function load(input:Array):void
		{
			temp = input.slice(0,input.length);
			
			amount = 100 / temp.length;
			
			get(temp[item]);
		}
		
		private function get(input:Object):void
		{
			item++;
			
			//free memory, but strange if it happens
			if(loader != null)
			{
				deconfigureListeners(loader.contentLoaderInfo);
				
				if(isLoading)
				{
					loader.close();
				}
				
				loader.unload();
			}
			loader = new Loader();
			configureListeners(loader.contentLoaderInfo);
			loader.load(new URLRequest(input.url));
			isLoading = true;
		}
		
		private function configureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.addEventListener(Event.COMPLETE, loaded);
            dispatcher.addEventListener(Event.INIT, initHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
        }
		
		private function deconfigureListeners(dispatcher:IEventDispatcher):void {
            dispatcher.removeEventListener(Event.COMPLETE, loaded);
            dispatcher.removeEventListener(Event.INIT, initHandler);
            dispatcher.removeEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
            dispatcher.removeEventListener(Event.OPEN, openHandler);
            dispatcher.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
        }

        private function initHandler(event:Event):void {
            trace("initHandler: " + event);
        }

        private function ioErrorHandler(event:IOErrorEvent):void {
            trace("Could not open photo: " + loader.contentLoaderInfo.url);
        }

        private function openHandler(event:Event):void {
            trace("openHandler: " + event);
        }

        private function progressHandler(event:ProgressEvent):void {
			progress = percent + ((event.bytesLoaded/event.bytesTotal)*amount);
			dispatchEvent(new Event(Event.CHANGE));
        }
		
		private function loaded(e:Event):void
		{
			isLoading = false;
			itemsLoaded++;
			percent = amount * itemsLoaded;
			
			items.push(loader.content);
			
			if(temp.length == item){
				//stop
				progress = 100;
				dispatchEvent(new Event(Event.CHANGE));
				dispatchEvent(new Event(Event.COMPLETE));
			}else{
				get(temp[item]);
			}
		}			
	}
}